package com.lepu.app.fun.health.adapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.app.utils.BitmapLoadUtil;
import com.app.utils.Const;
import com.app.utils.UmengEvent;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UmengHelper;
import com.lepu.app.fun.health.activity.RouteActivity;
import com.lepu.app.fun.health.bean.BeanDurg.Durg;
import com.lepu.app.fun.health.bean.BeanDurg.DurgStore;
import com.lepu.app.fun.health.bean.BeanMyAddress;
import com.lepu.app.main.activity.AppBroswerActivity;
import com.lepu.pasm.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<DurgStore> mArrayList = null;
	private BeanMyAddress mBeanMyAddress;

	public ListAdapter(Context context,ArrayList<DurgStore> arrayList , BeanMyAddress beanMyAddres) {

		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mBeanMyAddress = beanMyAddres;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "InflateParams", "NewApi" })
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.map_list_item, null);

			holder = new ViewHolder();

			
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.distance = (TextView) convertView.findViewById(R.id.distance);
			holder.address = (TextView) convertView.findViewById(R.id.address);
			holder.consult = (TextView) convertView.findViewById(R.id.consult);
			holder.go = (TextView) convertView.findViewById(R.id.go);
			
			holder.durgLayout1 = (LinearLayout) convertView.findViewById(R.id.durgLayout1);
			holder.durgLayout2 = (LinearLayout) convertView.findViewById(R.id.durgLayout2);
			holder.durgLayout3 = (LinearLayout) convertView.findViewById(R.id.durgLayout3);
			
			holder.durgImage1 = (ImageView) convertView.findViewById(R.id.durgImage1);
			holder.durgImage2 = (ImageView) convertView.findViewById(R.id.durgImage2);
			holder.durgImage3 = (ImageView) convertView.findViewById(R.id.durgImage3);
			
			holder.durgName1 = (TextView) convertView.findViewById(R.id.durgName1);
			holder.durgName2 = (TextView) convertView.findViewById(R.id.durgName2);
			holder.durgName3 = (TextView) convertView.findViewById(R.id.durgName3);
			
			holder.durgPrice1 = (TextView) convertView.findViewById(R.id.durgPrice1);
			holder.durgPrice2 = (TextView) convertView.findViewById(R.id.durgPrice2);
			holder.durgPrice3 = (TextView) convertView.findViewById(R.id.durgPrice3);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		

		final DurgStore item = mArrayList.get(position);

		holder.name.setText(item.InstitutionName);
		if(Integer.valueOf(item.Distance) >= 1000){
			double dbDis = (Double.valueOf(item.Distance))/1000;
			BigDecimal bd = new  BigDecimal(dbDis);
			double distance = bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();  
			holder.distance.setText(distance+"千米");
		}else{
			holder.distance.setText(item.Distance+"米");
		}
		
		holder.address.setText(item.Address);
		holder.consult.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				UmengHelper.CustomEvent(mContext, UmengEvent.event_fuwu_kuaisuzixun_yuanchenghuizhen);

				Intent intent = new Intent(mContext, AppBroswerActivity.class);
				intent.putExtra("title", "远程预约");
				intent.putExtra("from_type", Const.AppBroswerType.TYPE_YUAN_CHENG_HUI_ZHEN);
				intent.putExtra("url", "http://dl.ixinzang.com/pasm/onlineSeeDoctor/index.html");
				((BaseFragmentActivity)mContext).startActivity(intent,true);			
			}
		});
		
		holder.go.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(mContext,RouteActivity.class);
				
				Bundle bundle1 = new Bundle();
				bundle1.putSerializable("durgStore", item);
				intent.putExtras(bundle1);
				
				Bundle bundle2 = new Bundle();
				bundle2.putSerializable("myAddress", mBeanMyAddress);
				intent.putExtras(bundle2);
				((BaseFragmentActivity)mContext).startActivity(intent,true);
			}
		});
		
		if(item.MedicineList != null){
			
			holder.durgLayout1.setVisibility(View.GONE);
			holder.durgLayout2.setVisibility(View.GONE);
			holder.durgLayout3.setVisibility(View.GONE);
			
			for(int i=0; i<item.MedicineList.size(); i++){
				Durg durg = item.MedicineList.get(i);
				if(durg.ImgUrl != null){
					durg.ImgUrls = (List<String>) Arrays.asList(durg.ImgUrl.split("\\|"));
				}
				
				if(i == 0){
					holder.durgLayout1.setVisibility(View.VISIBLE);
					holder.durgName1.setText(durg.GenericName);
					holder.durgPrice1.setText("￥ "+durg.Price);
					
					if(durg.ImgUrls != null){
						BitmapLoadUtil.displayImage(R.drawable.default_icon_small,durg.ImgUrls.get(0),holder.durgImage1);
					}
					
				}else if(i == 1){
					holder.durgLayout2.setVisibility(View.VISIBLE);
					holder.durgName2.setText(durg.GenericName);
					holder.durgPrice2.setText("￥ "+durg.Price);
					
					if(durg.ImgUrls != null){
						BitmapLoadUtil.displayImage(R.drawable.default_icon_small,durg.ImgUrls.get(0),holder.durgImage2);
					}
				}else if(i == 2){
					holder.durgLayout3.setVisibility(View.VISIBLE);
					holder.durgName3.setText(durg.GenericName);
					holder.durgPrice3.setText("￥ "+durg.Price);
					
					if(durg.ImgUrls != null){
						BitmapLoadUtil.displayImage(R.drawable.default_icon_small,durg.ImgUrls.get(0),holder.durgImage3);
					}
				}
				
			}
		}

		return convertView;
	}
	
	private static class ViewHolder {
		TextView name;
		TextView distance;
		TextView address;
		TextView consult;
		TextView go;
		
		LinearLayout durgLayout1;
		LinearLayout durgLayout2;
		LinearLayout durgLayout3;
		
		ImageView durgImage1;
		ImageView durgImage2;
		ImageView durgImage3;
		
		TextView durgName1;
		TextView durgName2;
		TextView durgName3;
		
		TextView durgPrice1;
		TextView durgPrice2;
		TextView durgPrice3;
	}
}
